home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / mg2a_src.zip / TERMLIB / TTEST.C < prev   
C/C++ Source or Header  |  1988-08-23  |  8KB  |  370 lines

  1. /************************************************************************
  2.  *                                      *
  3.  *              Copyright (c) 1982, Fred Fish              *
  4.  *              All Rights Reserved                  *
  5.  *                                      *
  6.  *    This software and/or documentation is released for public          *
  7.  *    distribution for personal, non-commercial use only.          *
  8.  *    Limited rights to use, modify, and redistribute are hereby      *
  9.  *    granted for non-commercial purposes, provided that all          *
  10.  *    copyright notices remain intact and all changes are clearly     *
  11.  *    documented.  The author makes no warranty of any kind with      *
  12.  *    respect to this product and explicitly disclaims any implied    *
  13.  *    warranties of merchantability or fitness for any particular     *
  14.  *    purpose.                                  *
  15.  *                                      *
  16.  ************************************************************************
  17.  */
  18.  
  19.  
  20.  
  21. /*
  22.  *  TEST PROGRAM
  23.  *
  24.  *    testtcp    test termcap functions
  25.  *
  26.  *  KEY WORDS
  27.  *
  28.  *    test routines
  29.  *    termcap test
  30.  *
  31.  *  SYNOPSIS
  32.  *
  33.  *    termcap [-efns] terminal [capability [capability ...]]
  34.  *
  35.  *          -e  =>   expand string capability given by -s
  36.  *          -f  =>   determine boolean capabilities for terminal
  37.  *          -n  =>   determine numeric capabilities for terminal
  38.  *          -s  =>   determine string capabilities for terminal
  39.  *
  40.  *          terminal =>  terminal name as given in termcap file
  41.  *          capability => a boolean, numeric, or string capability
  42.  *
  43.  *          NOTE:  All capabilities must be of same type, as
  44.  *             given by [-fns].
  45.  *
  46.  *          If terminal is only argument then entire entry is
  47.  *          printed.
  48.  *
  49.  *  DESCRIPTION
  50.  *
  51.  *    Provides way to test termcap functions.  Can find
  52.  *    and print an entire termcap terminal entry, or various
  53.  *    capabilities from the entry.
  54.  *
  55.  *  AUTHOR
  56.  *
  57.  *    Fred Fish
  58.  *
  59.  */
  60.  
  61. #include <stdio.h>
  62.  
  63. #define TRUE 1
  64. #define FALSE 0
  65. #define NO_FILE           -1              /* Returned if can't open file */
  66. #define NO_ENTRY  0              /* Returned if can't find entry */
  67. #define SUCCESS      1              /* Returned if entry found ok */
  68. #define TRUNCATED 2              /* Returned if entry found but trunc */
  69. #define BUFFER_SIZE 1024
  70.  
  71. int eflag = FALSE;
  72. int fflag = FALSE;
  73. int nflag = FALSE;
  74. int sflag = FALSE;
  75.  
  76. int got_terminal = FALSE;
  77. int got_capability = FALSE;
  78.  
  79. int ospeed = 15;      /* fake lots of padding */
  80.  
  81.  
  82. /*
  83.  *  FUNCTION
  84.  *
  85.  *    main   termcap test entry point
  86.  *
  87.  *  KEY WORDS
  88.  *
  89.  *    main
  90.  *
  91.  *  SYNOPSIS
  92.  *
  93.  *    main(argc,argv)
  94.  *    int argc;
  95.  *    char *argv[];
  96.  *
  97.  *  DESCRIPTION
  98.  *
  99.  *    This is where the termcap test starts executing.    All argument list
  100.  *    switches are processed first, then all the specified
  101.  *    capability identification strings are processed.
  102.  *
  103.  */
  104.  
  105.  
  106. /*
  107.  *  PSEUDO CODE
  108.  *
  109.  *    Begin main
  110.  *      Process command line options.
  111.  *      For each argument list field
  112.  *          If field was not erased during option processing
  113.  *          If terminal name field not yet processed then
  114.  *              Process an assumed terminal name field.
  115.  *              Set terminal name processed flag.
  116.  *          Else
  117.  *              Process a capability field.
  118.  *              Set capability field processed flag.
  119.  *          End if
  120.  *          End if
  121.  *      End for
  122.  *      If no capabilities processed then
  123.  *          Simply dump buffer.
  124.  *      End if
  125.  *    End main
  126.  *
  127.  */
  128.  
  129. main(argc, argv)
  130. int argc;
  131. char *argv[];
  132. {
  133.     char *argp;
  134.     int argnum;
  135.     char buffer[BUFFER_SIZE];
  136.  
  137.     options(argc,argv);
  138.     for (argnum = 1; argnum < argc; argnum++) {
  139.     if ((argp = argv[argnum]) != NULL) {
  140.       if (!got_terminal) {
  141.           terminal(buffer,argp);
  142.           got_terminal = TRUE;
  143.       } else {
  144.           capability(argp);
  145.           got_capability = TRUE;
  146.       }
  147.     }
  148.     }
  149.     if (got_terminal && !got_capability) {
  150.       printf("size = %d\n%s",strlen(buffer),buffer);
  151.     }
  152. }
  153.  
  154.  
  155. /*
  156.  *  FUNCTION
  157.  *
  158.  *    options    process command line options
  159.  *
  160.  *  SYNOPSIS
  161.  *
  162.  *    options(argc,argv)
  163.  *    int argc;
  164.  *    char *argv[];
  165.  *
  166.  *  DESCRIPTION
  167.  *
  168.  *    Scans argument list, processing each switch as it is
  169.  *    found.  The pointer to each switch string is then
  170.  *    replaced with a NULL to effectively erase the switch
  171.  *    argument.
  172.  *
  173.  */
  174.  
  175.  
  176. /*
  177.  *  PSEUDO CODE
  178.  *
  179.  *    Begin options
  180.  *      For each argument in the argument list
  181.  *          Get pointer to first char of argument.
  182.  *          If the argument is a switch then
  183.  *          Replace argument pointer with NULL.
  184.  *          Look at next argument character.
  185.  *          While there is another argument character
  186.  *              Switch on the argument character
  187.  *              Case "EXPAND":
  188.  *              Set expand (e) flag.
  189.  *              Break out of switch.
  190.  *              Case "BOOLEAN":
  191.  *              Set boolean (f) flag.
  192.  *              Break out of switch.
  193.  *              Case "NUMERIC":
  194.  *              Set numeric flag.
  195.  *              Break out of switch.
  196.  *              Case "STRING":
  197.  *              Set string flag.
  198.  *              Break out of switch.
  199.  *              Default:
  200.  *              Abort with usage message.
  201.  *              End switch
  202.  *          End while
  203.  *          End if
  204.  *      End for
  205.  *    End options
  206.  *
  207.  */
  208.  
  209.  
  210. options(argc, argv)
  211. int argc;
  212. char *argv[];
  213. {
  214.     int i;
  215.     char c;          /* 1st char of current command-line argument */
  216.     char *cp;          /* current argument pointer */
  217.  
  218.     for (i=1; i<argc; i++) {
  219.     cp = argv[i];
  220.     if (*cp == '-') {
  221.         argv[i] = NULL;
  222.       cp++;
  223.       while (c = *cp++) {
  224.           switch (c) {
  225.           case 'e':
  226.           eflag = TRUE;
  227.           break;
  228.           case 'f':
  229.           fflag = TRUE;
  230.           break;
  231.           case 'n':
  232.           nflag = TRUE;
  233.           break;
  234.           case 's':
  235.           sflag = TRUE;
  236.           break;
  237.           default:
  238.           usage();
  239.           }
  240.         }
  241.     }
  242.     }
  243. }
  244.  
  245.  
  246. /*
  247.  *  FUNCTION
  248.  *
  249.  *    usage   give usage message and abort
  250.  *
  251.  *  KEY WORDS
  252.  *
  253.  *    usage
  254.  *    help processing
  255.  *    abort locations
  256.  *
  257.  *  SYNOPSIS
  258.  *
  259.  *    usage()
  260.  *
  261.  *  DESCRIPTION
  262.  *
  263.  *    Usage is typically called when a problem has been
  264.  *    detected in the argument list.
  265.  *    It prints a usage message and exits.
  266.  *
  267.  */
  268.  
  269.  
  270. /*
  271.  *  PSEUDO CODE
  272.  *
  273.  *    Begin usage
  274.  *      Print usage message.
  275.  *      Exit.
  276.  *    End usage
  277.  *
  278.  */
  279.  
  280. usage()
  281. {
  282.     printf("Usage: termcap [-fns] terminal [capability [capability ... ]]\n");
  283.     exit();
  284. }
  285.  
  286.  
  287.  
  288. terminal(buffer,name)
  289. char *buffer;
  290. char *name;
  291. {
  292.     int status;
  293.  
  294.     status = tgetent(buffer,name);
  295.     switch (status) {
  296.     case NO_FILE:
  297.       fprintf(stderr,"Can't find a termcap data base file.\n");
  298.       exit();
  299.     case NO_ENTRY:
  300.       fprintf(stderr,"Can't find entry \"%s\"\n",name);
  301.       exit();
  302.     case TRUNCATED:
  303.       fprintf(stderr,"Warning --- entry \"%s\" too long\n",name);
  304.       break;
  305.     case SUCCESS:
  306.     break;
  307.     default:
  308.     fprintf(stderr,"? tgetent returned illegal status %d\n",status);
  309.       exit();
  310.     }
  311. }
  312.  
  313.  
  314. capability(id)
  315. char *id;
  316. {
  317.     int value;
  318.     char buffer[256];
  319.     char *area;
  320.     char *ep, *tgoto();
  321.  
  322.     if (fflag) {
  323.       value = tgetflag(id);
  324.       if (value) {
  325.       printf("%s TRUE\n",id);
  326.       } else {
  327.       printf("%s FALSE\n",id);
  328.       }
  329.     } else if (nflag) {
  330.       value = tgetnum(id);
  331.       printf("%s = %o octal %d decimal\n",id,value,value);
  332.     } else if (sflag) {
  333.       area = buffer;
  334.       tgetstr(id,&area);
  335.       if (eflag) {
  336.       ep = tgoto(buffer,75,23);
  337.       }
  338.       doprint(id,buffer);
  339.       if (eflag) {
  340.       doprint(id,ep);
  341.       ep = tgoto(buffer,1,2);
  342.       doprint(id,ep);
  343.       }
  344.     }
  345. }
  346.  
  347.  
  348. /*
  349.  *  Use tputs to get a clearer picture of exactly what
  350.  *  goes out to the terminal....
  351.  */
  352.  
  353. princ(c)
  354. int c;
  355. {
  356.       if (c < 040)
  357.       printf("^%c",c |= 0100);
  358.       else
  359.       printf("%c",c);
  360. }
  361.  
  362. doprint(id,cp)
  363. char *id;
  364. char *cp;
  365. {
  366.     printf("%s = \"",id);
  367.     tputs(cp, 1, princ);
  368.     printf("\"\n");
  369. }
  370.